package gameheartv2;
import java.net.*;
import java.util.Vector;

class UDPClient implements Runnable
{
    private Thread thread = null;
    public Vector<String> vdata;
  
     static private int GetPosition(String source,char comp)
    {
         int lenght = source.length();
         for(int index = lenght-1; index>=0 ;index --)
         {
             if( source.charAt(index) == comp )
             {
                 return index;
             }
         }
         return -1;
     }

    static private String GetBroadCastIP()
    {
         String ketQua = null;
         try
              {
                InetAddress ipAddress =InetAddress.getLocalHost();
                ketQua = ipAddress.toString();
                int position = GetPosition(ketQua,'.');
                ketQua = ketQua.substring(0,position);
                ketQua = ketQua.concat(".255");
                position =GetPosition(ketQua,'/');
                if(position != -1)
                {
                    ketQua = ketQua.substring(position+1);
                }
              }
              catch(Exception ioe)
              {
                  System.out.println("Error : " + ioe.getMessage());
              }
         return ketQua;
     }

    public void run()  {
        try
        {
          DatagramSocket clientSocket = new DatagramSocket();
      String bcIp = GetBroadCastIP();
      InetAddress IPAddress = InetAddress.getByName(bcIp);
      byte[] sendData = new byte[1024];
      byte[] receiveData = new byte[1024];
      sendData = "1".getBytes();
      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
      clientSocket.send(sendPacket);
      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
      MyTableModel tableModel;
      
      for(int i =0;i<10;i++)
      {
          Vector<String> row = new Vector<String>();
          tableModel = new MyTableModel(GameStateRoom.colums,GameStateRoom.data);
          GameStateRoom.hostTable.setModel(tableModel);
          //GameStateRoom.hostTable.getColumnModel().getColumn(0).setPreferredWidth(95);
          GameStateRoom.hostTable.getColumnModel().getColumn(1).setPreferredWidth(520);
          clientSocket.receive(receivePacket);
          InetAddress IPHostAddress = receivePacket.getAddress();
          int stt = GameStateRoom.data.size()+1;
          row.add(Integer.toString(stt) );
          row.add(IPHostAddress.toString());
          GameStateRoom.data.add(row);
          String modifiedSentence = new String(receivePacket.getData());
          modifiedSentence = modifiedSentence.trim();
          System.out.println("FROM SERVER:" + modifiedSentence +" " +IPHostAddress.toString());
       }
      clientSocket.close();
        }
        catch(Exception ex)
        {

        }
    }

    public void start() {
        if (thread == null) {
            thread = new Thread(this);
            thread.start();
        }
   }

    public void stop() {
        if (thread != null) {
            thread.stop();
            thread = null;
        }
    }
//   public static void main(String args[]) throws Exception
//   {
//      DatagramSocket clientSocket = new DatagramSocket();
//      String bcIp = GetBroadCastIP();
//      InetAddress IPAddress = InetAddress.getByName(bcIp);
//      byte[] sendData = new byte[1024];
//      byte[] receiveData = new byte[1024];
//      sendData = "1".getBytes();
//      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
//      clientSocket.send(sendPacket);
//      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
//      MyTableModel tableModel;
//      for(int i =0;i<3;i++)
//      {
//          clientSocket.receive(receivePacket);
//          InetAddress IPHostAddress = receivePacket.getAddress();
//          String modifiedSentence = new String(receivePacket.getData());
//          modifiedSentence = modifiedSentence.trim();
//          System.out.println("FROM SERVER:" + modifiedSentence +" " +IPHostAddress.toString());
//       }
//      clientSocket.close();
//   }
}